/**
 * Copyright HCP Technology 
 * www.hcp.vn
 */
package controllers;

import java.util.ArrayList;
import java.util.List;

import models.ContentModel;
import models.FilterContentData;
import models.UserModel;
import play.Logger;
import play.cache.Cache;
import util.PaginationInfo;
import util.SimpleCache;
import util.Util;

public class Admin extends Application{
	
	private static void checkUserIsAdmin(){
		if(getUser() != null){
			UserModel userModel = UserModel.getUserModelByEmail(getUser());
			Logger.info("--do user id admin ?? >> " + userModel.isAdmin);
			if(!userModel.isAdmin){
				renderTemplate(Util.HOME_FO);
			}	
		}else{
			renderTemplate(Util.HOME_FO);
		}
	}
	
	public static void user(int page){
		checkUserIsAdmin();
		//TODO pls set cache for users
		//List<UserModel> users = Cache.get("users", List.class);
		List<UserModel> usersTotal = UserModel.getAll();
		List<UserModel> users = null;
		if(page == 0){
			page = 1;
		}
		users = UserModel.findByLimit(page, Util.PAGE_ITEM);
		Logger.info(" user page >> " + users.size());
		int total = usersTotal.size();
		
		PaginationInfo paginationInfo = new PaginationInfo(
				null,
				"Admin.user", // action
				page, // current page
				Util.PAGE_ITEM, // items per page
				total, // total number of results
				new String[]{ "query", "someParam" } // query params to be included in the links
			);
		render(users, paginationInfo);
		
	}
	
	public static void content(int page){
		checkUserIsAdmin();
		List<ContentModel> totalContents = ContentModel.getAll();
		int total = totalContents.size();
		Logger.info("content size >> " + totalContents.size());
		List<ContentModel> contents = null;
		if(page == 0){
			page = 1;
		}
		contents = getListContentLimit(totalContents, page, Util.PAGE_ITEM);	
		Logger.info("fetchContents >> " + contents.size());
		PaginationInfo paginationInfo = pagingInAdmin(page, total, "Admin.content");
		render(contents, paginationInfo);
	}
	/*list contents active*/
	public static void contentActive(int page){
		checkUserIsAdmin();
		List<ContentModel> totalContents = ContentModel.listNewContent(true, 0);
		Logger.info("content size >> " + totalContents.size());
		int total = totalContents.size();
		List<ContentModel> contents = null;
		if(page == 0){
			page = 1;
		}
		contents = getListContentLimit(totalContents, page, Util.PAGE_ITEM);	
		Logger.info("fetchContents >> " + contents.size());
		PaginationInfo paginationInfo = pagingInAdmin(page, total, "Admin.contentActive");
		renderTemplate(Util.ADMIN_CONTENT, contents, paginationInfo);
	}
	/*list contents de-active*/
	public static void contentDeActive(int page){
		checkUserIsAdmin();
		List<ContentModel> totalContents = ContentModel.listNewContent(false, 0);
		Logger.info("content size >> " + totalContents.size());
		int total = totalContents.size();
		List<ContentModel> contents = null;
		if(page == 0){
			page = 1;
		}
		contents = getListContentLimit(totalContents,page, Util.PAGE_ITEM);	
		Logger.info("fetchContents >> " + contents.size());
		PaginationInfo paginationInfo = pagingInAdmin(page, total, "Admin.contentDeActive");
		renderTemplate(Util.ADMIN_CONTENT, contents, paginationInfo);
	}
	/*active content*/
	public static void activeContent(Long id){
		checkUserIsAdmin();
		ContentModel content = (ContentModel)Cache.get("content_" + id);
		if( content == null){
			Logger.info("-------dont find info in Cache-----");
			content = ContentModel.findById(id, false);
		}
		content.active = true;
		content.update();
		Cache.set("content_" + id, content);
		List<ContentModel> listContentModels = ContentModel.findByCategory(content.category, true);
		SimpleCache.delete("category_" + content.category);
		SimpleCache.set("category_" + content.category, listContentModels);
		renderText(Util.SUCCESS);
	}
	/*de-active content*/
	public static void deActiveContent(Long id){
		checkUserIsAdmin();
		ContentModel content = (ContentModel)Cache.get("content_" + id);
		if( content == null){
			Logger.info("-------dont find info in Cache-----");
			content = ContentModel.findById(id, true);
		}else{
			Cache.delete("content_" + id);
		}
		content.active = false;
		content.update();
		Cache.set("content_" + id, content);
		List<ContentModel> listContentModels = ContentModel.findByCategory(content.category, true);
		SimpleCache.delete("category_" + content.category);
		SimpleCache.set("category_" + content.category, listContentModels);
		Logger.info("-------content is-----" + content.active);
		renderText(Util.SUCCESS);
	}
	

	public static void deleteContent(Long id){
		checkUserIsAdmin();
		ContentModel content = (ContentModel)Cache.get("content_" + id);
		if( content == null){
			Logger.info("-------dont find info in Cache-----");
			content = ContentModel.findById(id);
		}
		String category = content.category;
		ContentModel.deleteContent(id);
		Cache.delete("content_" + id);
		List<ContentModel> listContentModels = ContentModel.findByCategory(content.category, true);
		Cache.delete("category_" + category);
		Cache.set("category_" + category, listContentModels);
		renderText(Util.SUCCESS);
	}
	
	public static void activeUser(Long id){
		checkUserIsAdmin();
		UserModel userModel = UserModel.getUserById(id);
		if(userModel != null){
			UserModel.setActiveUser(userModel);
			renderText(Util.SUCCESS);
		}
		renderText(Util.FAIL);
	}
	
	public static void deActiveUser(Long id){
		checkUserIsAdmin();
		UserModel userModel = UserModel.getUserById(id);
		if(userModel != null){
			UserModel.setDeactiveUser(userModel);
			renderText(Util.SUCCESS);
		}
		renderText(Util.FAIL);
	}
	
	public static void setAdmin(Long id){
		checkUserIsAdmin();
		UserModel userModel = UserModel.getUserById(id);
		if(userModel != null){
			UserModel.setAdminToUser(userModel);
			renderText(Util.SUCCESS);
		}
		renderText(Util.FAIL);
	}
	
	private static PaginationInfo pagingInAdmin(int page, int total, String action){
		PaginationInfo paginationInfo = new PaginationInfo(
				null,
				action, // action
				page, // current page
				Util.PAGE_ITEM, // items per page
				total, // total number of results
				new String[]{ "query", "someParam" } // query params to be included in the links
			);
		return paginationInfo;
	}
	
	private static List<ContentModel> getListContentLimit(List<ContentModel> listContentModelTotals, int page, int limit){
		List<ContentModel> rs = new ArrayList<ContentModel>();
		for(int i = (page - 1)* limit; i<page*limit; i++){
			if(i < listContentModelTotals.size()){
				rs.add(listContentModelTotals.get(i));	
			}
		}
		return rs;
	}
	
	
	public static void filter(String data) {
		List<FilterContentData> list =  FilterContentData.getFilterContent();
		System.out.println("List size>>>>>>>>>>>" + list.size());
		System.out.println("List>>>>>>>>>>>" + data);
		SimpleCache.updateDataFilterContent(data);
		if((list == null || list.size() == 0) && data != null) {
			FilterContentData filter = new FilterContentData();
			filter.data = data;
			filter.save();
			System.out.println(">>>>>>>>>>save content");
			render(filter);
		}
		if(list != null && list.size() != 0 && data != null) {
			FilterContentData filter = list.get(0);
			System.out.println(">>>>>>>>>>update content");
			filter.data = data;
			filter.update();
			render(filter);
		}
		if(list != null && list.size() != 0) {
			FilterContentData filter = list.get(0);
			System.out.println(">>>>>>>>>>render content" + filter.data);
			render(filter);
		}
		render();
	}
}
